package com.ukao.pad.dialog;

import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.widget.StateImageView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.pad.R.id.close_btn;

/**
 * Created by ukaodev on 2017/6/2.
 */

public class WifiPassDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.edit_name_et)
    EditText mNameEt;

    @BindView(R.id.edit_name_cancel)
    Button meCancel;
    @BindView(R.id.edit_name_submit)
    Button mSubmit;
    @BindView(close_btn)
    StateImageView closeBtn;
    @BindView(R.id.tv_title)
    TextView title;
    private ScanResult mScanResult;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScanResult = getArguments().getParcelable(ARG_PARAM1);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_wife_pass, container, false);
        unbinder = ButterKnife.bind(this, view);
        fillData();
        return view;
    }

    private void fillData() {
        if (!CheckUtils.isNull(mScanResult)){
            title.setText(mScanResult.SSID);
        }

}

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.edit_name_submit:
               int type = 0;
                if (mScanResult.capabilities.contains("WPA2") || mScanResult.capabilities.contains("WPA-PSK")) {
                    type =3 ;
                } else if (mScanResult.capabilities
                        .contains("WPA")) {
                    type =2 ;
                } else if (mScanResult.capabilities.contains("WEP")) {
                    type =1 ;
                }

                String  pass=mNameEt.getText().toString().trim();
                if (pass.length()<8){
                  T.show("密码最少8位");
                    return;
                }
                Map  mMap=new HashMap();
                mMap.put(ARG_PARAM1,mScanResult.SSID);
                mMap.put(ARG_PARAM2,pass);
                mMap.put(ARG_PARAM3,type);
                if (mOnConfirmClickListener!=null){
                    mOnConfirmClickListener.onChildItemClick(R.id.edit_name_submit,mMap);
                }
                dismiss();
                break;
        }
    }


}
